<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employ" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmploy } from '@/api/employ'
import { postMents, putMents, xiuMents } from '@/api/dement'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isemit: {
      type: Boolean,
      required: true
    },
    orignList: {
      type: Array,
      required: true
    }
  },
  data() {
    const orign = (rule, value, callback) => {
      let codelist = null
      if (this.isemit) {
        codelist = this.orignList.filter(item => item.id !== this.id).map(({ code }) => code)
      } else {
        codelist = this.orignList.map(({ code }) => code)
      }

      if (codelist.includes(value)) {
        callback(new Error(value + '已存在'))
      } else {
        callback()
      }
    }
    const nameList = (rule, value, callback) => {
      let namelist = null
      // const namelist = this.orignList.filter(item => item.pid === this.id).map(({ name }) => name)
      if (this.isemit) {
        const pid = this.orignList.find(item => item.id === this.id).pid
        namelist = this.orignList.filter(item => item.pid === pid && item.id !== this.id).map(({ name }) => name)
      } else {
        namelist = this.orignList.filter(item => item.pid === this.id).map(({ name }) => name)
      }
      if (namelist.includes(value)) {
        callback(new Error(value + '已存在'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称要求2-10个字符', trigger: 'blur' },
          { validator: nameList, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码要求2-10个字符', trigger: 'blur' },
          { validator: orign, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employ: []
    }
  },
  created() {
    this.loadEmploy()
    this.loadEmit()
  },
  methods: {
    async loadEmit() {
      if (!this.isemit) return
      const res = await xiuMents(this.id)
      // console.log(res)
      this.form = res.data
    },
    async loadEmploy() {
      try {
        const res = await getEmploy()
        this.employ = res.data
      } catch (e) {
        console.log(e)
      }
    },

    hSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        let res = null
        try {
          if (this.isemit) {
            res = await putMents(this.form)
          } else {
            res = await postMents({ ...this.form, pid: this.id })
          }
          this.$message.success(res.message)
          this.$emit('succac')
        } catch (e) {
          this.$message.error(e.message)
        }
      })
    },
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>

